<?php
/**
* File that contains Application_Form_Admin_insertBusline class.
* @package busmap
*/

/**
* Application_Form_Admin_insertBusline class generates a form used to insert a new busline.
* Form has a text field for the name.
* Textarea for the description of the busline.
* There is a subform busstopsForm that allows to select busstops that this line goes through.
* @package busmap
*/
class Application_Form_Admin_insertBusline extends Zend_Form
{
	/**
	* Initialize function generates all the elements described in the class description.
	*/
    public function init()
    {
        $this->setMethod('post');

        $this->addElement(
            'text', 'name', array(
                'required' => true,
                'filters'    => array('StringTrim'),
            ));
		$this->name->addErrorMessage('Bussiliini nimi ei saa olla tühi');
		$this->name->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell'));
		

        $this->addElement(
            'textarea', 'description', array(
                'required' => true,
            ));
		$this->description->addErrorMessage('Bussiliini kirjeldus ei saa olla tühi');
		$this->description->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-big'));

		$busstopsForm = new Zend_Form_SubForm('busstopsForm');
		$busstopsForm->removeDecorator('fieldset');
		$busstopsForm->removeDecorator('DtDdWrapper');
		$busstopsForm->setName('busstopsForm');
		$busstopsForm->setAttrib('id','busstopsForm');
		$this->addSubForm($busstopsForm, 'busstopsForm');
		$this->busstopsForm->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell busstop-listing'));
		$this->populateWithBusstops(array());

	    $this->addElement('submit', 'addNew', array(
            'ignore'   => false,
            'label'    => 'Lisa',
			'class'    => 'submit-data',
			'onClick' => '"this.form.submit()"'
            ));
		$this->addNew->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button'));

	 
		$elements = $this->getElements();
		foreach($elements as $element){
			$element->removeDecorator('label')->removeDecorator('DtDdWrapper')->removeDecorator('Errors');;
		}

		
    }

	/**
	* This function populates the busstopsForm with the busstops sent to it.
	* It generates a new button before all and between each of the busstops. Using these buttons, admin can add a busstop to the corresponding place in the busstop list of the busline being added.
	*/
	public function populateWithBusstops($busstops){

		$this->busstopsForm->addElement('button', 'add0',
			array(
				'class'=>'add-new-busstop-between',
				'label'=>'LISA',
				'onClick'=>'showBusstopListing(-1, "new", this)'
			)
		);
		$this->busstopsForm->{'add0'}->removeDecorator('DtDdWrapper');
		if(count($busstops)){
			$busstopElements = array();
			foreach($busstops as $key => $busstop){
				if($busstop && $busstop->id){
					$uniqueId = $key+1;
					//list($busstop, $uniqueId) = $busstop_data;
					$this->busstopsForm->addElement('checkbox', strval($uniqueId),
						array(
							'label'=>$busstop->name,
							'belongsTo'=>'stops',
							'checkedValue'=>$busstop->id,
							'checked'=>true
						)
					);
					$this->busstopsForm->$uniqueId->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'buslines-admin-busstop-checkbox'));
					$this->busstopsForm->$uniqueId->addDecorator('Label', array('class'=>'buslines-admin-busstop-name', 'placement'=>'append'));

					$this->busstopsForm->addElement('button', 'add'.$uniqueId,
						array(
							'class'=>'add-new-busstop-between',
							'label'=>'LISA',
							'onClick'=>'showBusstopListing('.$uniqueId.', "new", this)'
						)
					);
					$this->busstopsForm->{'add'.$uniqueId}->removeDecorator('DtDdWrapper');
				}
			}

		}
	}

}